package com.iotechn.unimall.admin.api.newsCategory;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.NewsCategoryDO;
import com.iotechn.unimall.data.domain.NewsDO;
import com.iotechn.unimall.data.mapper.NewsCategoryMapper;
import com.iotechn.unimall.data.mapper.NewsMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class AdminNewsCategoryServiceImpl implements AdminNewsCategoryService {

    @Autowired
    private NewsCategoryMapper newsCategoryMapper;
    @Autowired
    private NewsMapper newsMapper;

    @Override
    public Page<NewsCategoryDO> list(Integer page, Integer limit, String name, Long adminId) throws ServiceException {
        Wrapper<NewsCategoryDO> wrapper = new EntityWrapper<NewsCategoryDO>();
        if (!StringUtils.isEmpty(name))
            wrapper.like("name", name);
        wrapper.orderBy("id", false);
        Integer count = newsCategoryMapper.selectCount(wrapper);
        List<NewsCategoryDO> NewsCategoryDOS = newsCategoryMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        return new Page<>(NewsCategoryDOS, page, limit, count);
    }

    @Override
    public List<NewsCategoryDO> tabList(Integer type,Long adminId) {
        Wrapper<NewsCategoryDO> wrapper = new EntityWrapper<NewsCategoryDO>().eq("type",type)
                .setSqlSelect("`id`", "`name`").orderBy("id", false);
        return newsCategoryMapper.selectList(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NewsCategoryDO create(NewsCategoryDO newsCategoryDO, Long adminId) throws ServiceException {
        Wrapper<NewsCategoryDO> wrapper = new EntityWrapper<NewsCategoryDO>()
                .eq("name", newsCategoryDO.getName());
        if (newsCategoryMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_CATEGORY_CANNOT_REPEATED);
        if (newsCategoryMapper.insert(newsCategoryDO) > 0) {
            newsCategoryDO.setId(newsCategoryDO.getId());
            return newsCategoryDO;
        }
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(NewsCategoryDO newsCategoryDO, Long adminId) throws ServiceException {
        Wrapper<NewsCategoryDO> wrapper = new EntityWrapper<NewsCategoryDO>()
                .eq("name", newsCategoryDO.getName())
                .ne("id", newsCategoryDO.getId());
        if (newsCategoryMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_CATEGORY_CANNOT_REPEATED);
        Long id = newsCategoryDO.getId();
        if (id == null) return "fail";
        if (newsCategoryMapper.updateById(newsCategoryDO) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String delete(Long id, Long adminId) throws ServiceException {
        Wrapper<NewsDO> wrapper = new EntityWrapper<NewsDO>().eq("category", id);
        if (newsMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_CATEGORY_CANNOT_DELETE);
        if (newsCategoryMapper.deleteById(id) > 0) return "ok";
        throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
    }

    @Override
    public NewsCategoryDO detail(Long id, Long adminId) throws ServiceException {
        return newsCategoryMapper.selectById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String batchDelete(String idsJson, Long adminId) throws ServiceException {
        List<Long> ids = JSONObject.parseArray(idsJson, Long.class);
        if (CollectionUtils.isEmpty(ids))
            throw new AdminServiceException(ExceptionDefinition.NEWS_CATEGORY_NOT_EXIST);
        Wrapper<NewsDO> wrapper = new EntityWrapper<NewsDO>().in("id", ids);
        if (newsMapper.selectCount(wrapper) > 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_CATEGORY_CANNOT_DELETE);
        if (newsCategoryMapper.delete(new EntityWrapper<NewsCategoryDO>().in("id", ids)) <= 0)
            throw new AdminServiceException(ExceptionDefinition.NEWS_CATEGORY_NOT_EXIST);
        return "ok";
    }

}
